*-------------------------------------------------------------------------
* Collapse PRISM data into annual and average data for the Growing Season
*------------------------------------------------------------------------
set more off
* range of years in analysis 
local yearMin = 1981
local initial_date = "01/01/`yearMin'"
local yearMax = 2012

* define growing season season
* first month and day of month used in a year
local monthBegin = 4 
local dayMonthBegin =  1
* last month and day of month used in a year
local monthEnd   = 9 
local dayMonthEnd   = 31



* Erase the old version of the data before starting loop to append each year of data
capture erase "..\temp\PRISM_county_annual_GrowSeason.dta"

* Average data within the growing season
forvalues t = `yearMin'/`yearMax' {
		display "`t'"
qui{		
        ******************************************************************************
        * load data
        use "..\temp\PRISM_WaterBalance_`t'", clear 
		local tMinus1=`t'-1
		drop if date==date("12/31/`tMinus1'", "MDY")
		
		* keep only days for selected part of year
        qui drop if (month < `monthBegin')
        qui drop if (month > `monthEnd')
        qui drop if (month == `monthBegin') & (day < `dayMonthBegin')
        qui drop if (month == `monthEnd')   & (day > `dayMonthEnd')
		
		drop ppt_snow ppt_rain ppt_remain smf snostor sm stw
		
		* Generate Soil Moisture Deficit
		gen soil_moist_deficit_usgs=rootznaws-soil_moisture_usgs
		gen soil_moist_deficit=rootznaws-soil_moisture
		gen soil_moist_deficit_corn=rootznaws-soil_moisture_corn

        
		
		* Calculate net precipitation
		gen netppt0=ppt-ET0 
		gen netpptc=ppt-ETc 
		

		
        ******************************************************************************;
        * collapse by year
        collapse (sum) ppt dday* ET0 ET0_elev ET0_Har ETc netppt* aet* water_deficit* water_surplus* ///
				(mean) soil_moist* tmin tmax tavg vpd, by(stcofips year)
		* ET0_elev was missing for some counties because elevation data was missing
		* so replace 0 with missing
		replace ET0_elev=. if ET0_elev==0
	
        ******************************************************************************;
        * append years and save data
		capture append using "..\temp\PRISM_county_annual_GrowSeason"
        sort stcofips year
        save "..\temp\PRISM_county_annual_GrowSeason", replace
} // end of quiet		
}


* Average across 1983-2012 to get normal (30-year) climate data
use "..\temp\PRISM_county_annual_GrowSeason", clear
keep if year>=1983 & year<=2012
collapse (mean) ppt dday* ET0 ET0_elev ET0_Har ETc netppt* tmin tmax tavg vpd ///
			aet* water_deficit* water_surplus* soil_moist*, by(stcofips)
* ET0_elev was missing for some counties because elevation data was missing
* so replace 0 with missing
replace ET0_elev=. if ET0_elev==0
		
save "..\temp\PRISM_county_climate83-12_GrowSeason", replace








*-------------------------------------------------------------------------
* Collapse PRISM data into annual and average data for off-season
*------------------------------------------------------------------------
set more off
* range of years in analysis 
local yearMin = 1982
local yearMax = 2012

* define winter
* first month and day of month used in a year
local monthBeginEarly = 1 
local dayMonthBeginEarly =  1
* last month and day of month used in a year
local monthEndEarly  = 3 
local dayMonthEndEarly   = 31

* first month and day of month used in a year
local monthBeginLate = 10 
local dayMonthBeginLate =  1
* last month and day of month used in a year
local monthEndLate   = 12 
local dayMonthEndLate   = 31

* append data across years so can get winter data that spans across calendar years
* Go backwards through years to drop December 31 from previous year in each dataset
clear
forvalues t = `yearMax'(-1)`yearMin' {
		display "`t'"
        ******************************************************************************
        capture append using "..\temp\PRISM_WaterBalance_`t'"
		local tMinus1=`t'-1
		drop if date==date("12/31/`tMinus1'", "MDY")
}

		* keep only days for selected part of year
		qui drop if (month > `monthEndEarly') & (month < `monthBeginLate')
		qui drop if (month == `monthBeginEarly') & (day < `dayMonthBeginEarly')
		qui drop if (month == `monthEndEarly')   & (day > `dayMonthEndEarly')
		qui drop if (month == `monthBeginLate') & (day < `dayMonthBeginLate')
		qui drop if (month == `monthEndLate')   & (day > `dayMonthEndLate')

		* The variable year indicates the winter months that precede the 
		* growing season for a particultur year
		replace year=year+1 if month>= `monthBeginLate'

		drop ppt_snow ppt_rain ppt_remain smf snostor sm stw
				
		* Generate Soil Moisture Deficit
		gen soil_moist_deficit_usgs=rootznaws-soil_moisture_usgs
		gen soil_moist_deficit=rootznaws-soil_moisture
		gen soil_moist_deficit_corn=rootznaws-soil_moisture_corn

        
		* Calculate net precipitation
		gen netppt0=ppt-ET0 
		gen netpptc=ppt-ETc 
		
		      * collapse by year
        collapse (sum) ppt ET0 ET0_elev ET0_Har ETc water_deficit* water_surplus* ///
				(mean) soil_moist* tmin tmax tavg vpd, by(stcofips year)
		* ET0_elev was missing for some counties because elevation data was missing
		* so replace 0 with missing
		replace ET0_elev=. if ET0_elev==0


	ren * *OffSeason
	ren yearOffSeason year
	ren stcofipsOffSeason stcofips

	save "..\temp\PRISM_county_annual_OffSeason", replace



* Average across 1983-2012 to get normal (30-year) climate data
use "..\temp\PRISM_county_annual_OffSeason", clear
keep if year>=`yearMin'+1 & year<=`yearMax'
collapse (mean) ppt* ET* tmin* tmax* tavg* vpd* ///
			water_deficit* water_surplus* soil_moist*, by(stcofips)		
save "..\temp\PRISM_county_climate83-12_OffSeason", replace
